Skip to content

Commit ebcd61c

Browse files
committed
Make more highlight improvements
1 parent 457cefb commit ebcd61c

File tree

5 files changed

+263
-43
lines changed

5 files changed

+263
-43
lines changed

llamafile/high.cpp

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,40 +48,7 @@ static std::string extname(const std::string_view path) {
4848
return std::string(name.substr(dot_pos + 1));
4949
}
5050

51-
int main(int argc, char *argv[]) {
52-
53-
// process flags
54-
int opt;
55-
int infd = 0;
56-
int outfd = 1;
57-
const char *lang = nullptr;
58-
const char *inpath = nullptr;
59-
while ((opt = getopt(argc, argv, "hl:o:")) != -1) {
60-
switch (opt) {
61-
case 'h':
62-
printf("usage: %s [-l LANG] [-o OUTFILE] [INFILE]\n", argv[0]);
63-
exit(0);
64-
case 'l':
65-
lang = optarg;
66-
break;
67-
case 'o':
68-
if ((outfd = creat(optarg, 0644)) == -1) {
69-
perror(optarg);
70-
exit(1);
71-
}
72-
break;
73-
default:
74-
exit(1);
75-
}
76-
}
77-
if (optind < argc) {
78-
inpath = argv[optind];
79-
if ((infd = open(inpath, O_RDONLY)) == -1) {
80-
perror(inpath);
81-
exit(1);
82-
}
83-
}
84-
51+
static void highlight(int infd, int outfd, const char *lang, const char *inpath) {
8552
// create syntax highlighter
8653
Highlight *h;
8754
const char *ext;
@@ -123,3 +90,44 @@ int main(int argc, char *argv[]) {
12390
H.flush(&res);
12491
write(outfd, res.data(), res.size());
12592
}
93+
94+
int main(int argc, char *argv[]) {
95+
96+
// process flags
97+
int opt;
98+
int outfd = 1;
99+
const char *lang = nullptr;
100+
while ((opt = getopt(argc, argv, "hl:o:")) != -1) {
101+
switch (opt) {
102+
case 'h':
103+
printf("usage: %s [-l LANG] [-o OUTFILE] [INFILE]\n", argv[0]);
104+
exit(0);
105+
case 'l':
106+
lang = optarg;
107+
break;
108+
case 'o':
109+
if ((outfd = creat(optarg, 0644)) == -1) {
110+
perror(optarg);
111+
exit(1);
112+
}
113+
break;
114+
default:
115+
exit(1);
116+
}
117+
}
118+
119+
// process files
120+
if (optind == argc) {
121+
highlight(0, outfd, lang, 0);
122+
} else {
123+
for (int i = optind; i < argc; ++i) {
124+
int infd;
125+
const char *inpath = argv[i];
126+
if ((infd = open(inpath, O_RDONLY)) == -1) {
127+
perror(inpath);
128+
exit(1);
129+
}
130+
highlight(infd, outfd, lang, inpath);
131+
}
132+
}
133+
}

llamafile/highlight.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,11 @@ class HighlightShell : public Highlight {
471471
int u_ = 0;
472472
int t_ = 0;
473473
int i_ = 0;
474+
int curl_ = 0;
475+
int last_ = 0;
474476
bool pending_heredoc_ = false;
475477
bool indented_heredoc_ = false;
478+
bool no_interpolation_ = false;
476479
std::string word_;
477480
std::string heredoc_;
478481
};

0 commit comments

Comments
 (0)